package topcbl.task;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import topcbl.data.Car;
import topcbl.net.TaxiService;
import topcbl.taxi.CallTaxiActivity;
import topcbl.taxi.MainActivity;
import topcbl.taxi.R;
import topcbl.util.StringUtils;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

public class GetNearCarTask extends AsyncTask<Void, Void, ArrayList<Car>> {
	Context context;
	TaxiService mService;
	// info
	double lat;
	double lng;
	int numberCars;
	int numberKm;
	int carType;

	public GetNearCarTask(Context context, double lat, double lng,
			int numberCars, int numberKm, int carType) {
		mService = MainActivity.mBindService.getService();
		this.lat = lat;
		this.lng = lng;
		this.numberCars = numberCars;
		this.numberKm = numberKm;
		this.carType = carType;
		this.context = context;
	}

	@Override
	protected void onPreExecute() {	
		super.onPreExecute();
		// show progress dialog
	}

	@Override
	protected ArrayList<Car> doInBackground(Void... params) {
		// wait in 3s
		try {
			Thread.sleep(3000);
		} catch (Exception e) {
		}
		HttpClient httpclient = new DefaultHttpClient();
		String url = "http://gps.smartsafe.vn/taxiws/Service1.svc/getNearCars?username=demo1&myLat="
				+ lat
				+ "&myLon="
				+ lng
				+ "&NumberCar="
				+ numberCars
				+ "&NumberKm=" + numberKm + "&CarType=" + carType;
		// String url =
		// "http://gps.smartsafe.vn/taxiws/Service1.svc/getLastPosition/1682387232/0";
		HttpResponse response;
		try {
			response = httpclient.execute(new HttpGet(url));
			String data = StringUtils.readAllFromInputStream(response
					.getEntity().getContent());
			return parseJSON(data);
		} catch (ClientProtocolException e) {
			Log.e("topcbl", e.getMessage());
		} catch (IOException e) {
			Log.e("topcbl", e.getMessage());
		}
		Toast.makeText(context,
				context.getString(R.string.network_unavailable),
				Toast.LENGTH_SHORT).show();
		return null;
	}

	@Override
	protected void onPostExecute(ArrayList<Car> result) {
		super.onPostExecute(result);
		if (result != null) {
			((CallTaxiActivity) context).takeNearCars(result);
			Toast.makeText(
					context,
					context.getString(R.string.toastFoundCar) + result.size()
							+ " xe", Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(context, context.getString(R.string.no_found_car),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
	}

	private ArrayList<Car> parseJSON(String data) {
		ArrayList<Car> result = new ArrayList<Car>();
		try {
			JSONArray arr = new JSONArray(data);
			if (arr.length() == 0)
				return null;
			for (int i = 0; i < arr.length(); i++) {
				JSONObject obj = arr.getJSONObject(i);
				result.add(new Car(obj.getDouble("dbLat"), obj
						.getDouble("dbLon"), obj.getString("strTEID")));
			}
		} catch (Exception e) {
			Log.e("topcbl", "Fail to parse JSON " + e.getMessage());
		}
		return result;
	}

}
